#include "FoodMushroom.h"


FoodMushroom::FoodMushroom(void)
{
}

FoodMushroom::FoodMushroom(float x, float y, float vx, float vy, int deltaTime,int resourceName,CResourceManager *rm):MObject(x,y,vx,vy,deltaTime,resourceName,rm)
{
	_objectType = CResourceManager::TYPE_FOOD;
	_isMovable = true;
	_spriteMushroom = rm->getSprite(CResourceManager::MUSHROOMFOOD);
	_spriteFlower = rm->getSprite(CResourceManager::FLOWER);
	_scored = 200;
	//_vx = 0.1f;
	//_status = STATUS_ALIVE;
}

void FoodMushroom::Move()
{
	switch (_status)
	{
	case STATUS_MUSHROOM:
		{
			Vector v = _lastTranslateVector;
			float t1 = _lastTranslateVector.getY();
			float t2 = this->getVelocityVector().getY();
			float ty = t1 + t2;
	 
			if (_isFalling == false || this->getLastTranslateVector() != Vector() &&(ty<=0))
				{
					_vy = 0; 
				}
				else
					if (_vy < 1.5f)
   						_vy += 0.15f;

			if (_y >600 - 32) _vy =0;

			_lastX = _x;
			_lastY = _y;

			_x +=_vx*_deltaTime;
			_y +=_vy*_deltaTime;

			_lastTranslateVector = Vector();
			break;
		}
	}
}

void FoodMushroom::Render()
{
	switch (_status)
	{
	case STATUS_MUSHROOM:
	case STATUS_FLOWER:
		{
			MObject::Render();
			break;
		}
	}
}

void FoodMushroom::setVelocity(Vector v, shared_ptr<MObject> Object)
{

}

void FoodMushroom::setStatus(int Status)
{
	_status = Status;
	switch (_status)
	{
	case STATUS_MUSHROOM:
		{
			_vx = 0.1f;
			_vy = 0.f;
			_sprite = _spriteMushroom;
			break;
		}

	case STATUS_FLOWER:
		{
			_vx = 0;
			_vy = 0;
			_sprite = _spriteFlower;
			break;
		}
	}
}

FoodMushroom::~FoodMushroom(void)
{
}

void FoodMushroom::actAfterCollided(int Direction, shared_ptr<MObject> Object)
{
	if (Object->getObjectType()/10 == CResourceManager::TYPE_MARIO)
	{
		setStatus(STATUS_DEAD);
		CMarioSound::getInstance()->Play(CMarioSound::SOUND_LEVELUP,false);

		//int i = Object->getCoin();
		//Object->setScore(Object->getScore() + 100);
		return;
	}

	if (_status == STATUS_FLOWER)
		return;

	switch(Direction)
	{
	case 1://UP
		{
			_vy = 0;
			if (Object->getObjectType() == CResourceManager::TYPE_BRICK && (Object->getStatus() == Brick::STATUS_SLIDE || Object->getStatus() == Brick::STATUS_BREAKED))
				_vx = -_vx;
			break;
		}
	case 2: //DOWN
		{
			break;
		}
	case 3: //RIGHT
	case 4: //LEFT
		{
			if (_recentChangeVel == false)
			{
				if (Object->getPosition().getY() <= _y + 20)
				{
					_vx = -_vx;
					_recentChangeVel = true;
				}
			}
			else _recentChangeVel = false;
		}
	case 0:
		break;
	}
}